package com.bdqn.easybuy1.service.user;

import com.bdqn.easybuy1.dao.UserDao;
import com.bdqn.easybuy1.dao.impl.UserDaoImpl;
import com.bdqn.easybuy1.entities.User;
import com.bdqn.easybuy1.utils.JDBCUtils;
import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public boolean add(User user) {
        Connection connection = null;
        int count = 0;
        try {
            connection = JDBCUtils.getConnection();
            UserDao userDao = new UserDaoImpl();
            count = userDao.insert(connection, user);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count > 0;
        }
    }

    @Override
    public boolean update(User user) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            UserDao userDao = new UserDaoImpl();
            count = userDao.update(connection, user);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count > 0;
        }
    }

    @Override
    public boolean deleteUserById(Integer userId) {
        Connection connection = null;
        Integer count = 0;
        try {
            connection = JDBCUtils.getConnection();
            UserDao userDao = new UserDaoImpl();
            count = userDao.deleteUserById(connection, userId + "");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count > 0;
        }
    }

    @Override
    public User getUser(Integer userId, String loginName) {
        Connection connection = null;
        User user = null;
        try {
            connection = JDBCUtils.getConnection();
            UserDao userDao = new UserDaoImpl();
            user = userDao.getUserById(connection, userId, loginName);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return user;
        }
    }

    @Override
    public List<User> getUserList(Integer startIndex, Integer pageSize) {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = JDBCUtils.getConnection();
            UserDao userDao = new UserDaoImpl();
            userList = userDao.getUserList(connection, startIndex, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return userList;
        }
    }

    @Override
    public int count() {
        Connection connection = null;
        Integer count = null;
        try {
            connection = JDBCUtils.getConnection();
            UserDao userDao = new UserDaoImpl();
            count = userDao.count(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeQuietly(connection);
            return count;
        }
    }
}
